<template>
  <el-dialog title="测评票预览" :fullscreen='true' :close-on-click-modal="false" :visible.sync="show" width="100%">
<div class="fontDa">
    <div class="customTableHeader">
      <div class="customTableTitle">{{ cppName }}</div>
    </div>

    <div class="customTableHeader">
      <div class="customTableTitle" style="display: flex;justify-content: center;"><div>可推荐人数：{{ recommendNum }}</div><div style="margin-left: 110px;">已推荐人数：{{ recommendNumRead }}</div></div>
    </div>
     <div class="zhaoPian">
      <div style="padding-bottom: 10px;">单位：{{sheetDept}}</div></div>
    </div>
    <div class="cpTable">
      <el-table ref="report-table" :data="tableData" :span-method="objectSpanMethod" border
                :row-class-name="tableRowClassName">
        <el-table-column v-for="col in this.colShowAll" :prop="col.prop" :label="col.label">
        </el-table-column>
        <el-table-column v-for="(gz,j) in recommendList" :key="j+10000" :label="gz.fzMc"
                         :fzIndex="j" label="是否推荐" align="center" min-width="50">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-success" class="cpCheck" v-if="scope.row.userName==gz.username"></el-button>
            <el-button type="text" class="cpCheck" v-else><i class="noChecked"></i>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="width: 100%;margin-top:30px;" class="zfheaderbox">
        <div>{{footerText}}</div>
      </div>
      <!-- <el-button @click="exportExcel(111)">导出</el-button> -->
    </div>
</div>
  </el-dialog>
</template>

<script>
import {xpInfo, daFenCp, daFenDj} from "@/api/ceping/cepinghdglxpxxb";
import {getUserList, getUserListByDept} from "@/api/ceping/cepingcpppypzb";
export default {
  name: "cpIndexXp",
  data() {
    return {
      aa:"",
      shuxing:"top-end",
      show: false,
      df: true,
      cppName: '',
      sheetDept:'',
      cepingHdglXpxxb: {},
      cepingHdglb: {},
      cepingHdglXpxxDfjlbList: [],
      cepingXmmxbList: [],
      cepingGzmxbList: [],
      cepingCpgzBcpmxb: {},
      cepingCpgzCpmxb: {},
      cepingCpgzCpmxCppzb: {},
      cepingDjmxbList: [],
      cepingCpgzBcpmxbUser: {},
      cepingCpgzBcpmxbDept: {},
      cepingXmmxbObj: {},
      fzbList: [],
      zbCj: 2,//指标层级
      tableColumn: [],
      tableHeader: [],
      tableData: [],//指标结构
      cepingDjmxb: {},
      ksdialogVisible: false,
      colShowAll:[],
      footerText:'',
      recommendList:[{'username':''}],
      recommendNum:0,
      recommendNumRead:0
    }
  },
  methods: {
    initConfig(id) {
      this.show = false;
      this.df = true;

    },

    initXp(r) {
     //console.log(r)
      this.tableData = [];
      this.show = false;
      this.df = true;
      this.$nextTick(() => {
        this.cppName = r.cppName;
        this.footerText = r.footerText;
        this.sheetDept = r.sheetDept;
        //选票信息
        // this.cepingHdglXpxxb = res.data.data.cepingHdglXpxxb;
        //会议信息
        // this.cepingHdglb = res.data.data.cepingHdglb;
        //打分列表
        // this.cepingHdglXpxxDfjlbList = res.data.data.cepingHdglXpxxDfjlbList;
        //项目明细
        if (r.xmxxXmmx) {
          this.cepingXmmxbList = JSON.parse(r.xmxxXmmx)
        }
        if (r.gzxxGzmx) {
          //规则明细
          this.cepingGzmxbList = JSON.parse(r.gzxxGzmx)
        }
        //人员动态列
        if(r.hostConfigJson){
          this.colShowAll = JSON.parse(r.hostConfigJson);
        }
        //获取被评测人员信息
        let ids = '';
        if(r.cpgzBcpmxb){
          let ryList = JSON.parse(r.cpgzBcpmxb);
          var userids=[],deptids=[],usreridstr='',deptidsstr="";
          ryList.forEach(item=>{
            if(item.userId){
              userids.push(item.userId)
            }else{
              deptids.push(item.deptId)
            }
          });
          usreridstr=userids.join(',');
          deptidsstr=deptids.join(',');
          // for (var i = 0; i < ryList.length; i++) {
          //   ids += ryList[i].userId + ',';
          // }
        }
        var userTableData=[],deptTableData=[];
        if(usreridstr){
          getUserList(usreridstr).then(response => {
            userTableData = response.data.data
            this.$nextTick(()=>{
              this.tableData = [...userTableData,...deptTableData]
              this.tableData.forEach(item=>{
                if(item.rySr){
                  item.rySr = item.rySr.substring(0,7)
                }
              })
              // console.log('11111111111',JSON.stringify(this.tableData))
            })
          })
        }
        if(deptidsstr){
          getUserListByDept(deptidsstr).then(response => {
            deptTableData=response.data.data;
            this.$nextTick(()=>{
              this.tableData = [...userTableData,...deptTableData]
              // console.log('333333',this.tableData)
            })
            // console.log('222222222222222222',deptTableData)
          })
        }
        // getUserList(ids).then(response => {
        //  //console.log(response)
        //   this.tableData = response.data.data
        // })
        //可推荐人数
        this.recommendNum = r.recommendNum;
        //被测评对象
        // this.cepingCpgzBcpmxb = res.data.data.cepingCpgzBcpmxb;
        this.cepingDjmxbList = [];
        if (r.djxxDjmx) {
          this.cepingDjmxbList = JSON.parse(r.djxxDjmx)
        }

        //用户
        // this.cepingCpgzBcpmxbUser = res.data.data.cepingCpgzBcpmxbUser;
        //部门
        // this.cepingCpgzBcpmxbDept = res.data.data.cepingCpgzBcpmxbDept;
        this.init();

      });


    },
    init() {
      this.cepingXmmxbObj = {};
      this.zbCj = 1;
      for (var i in this.cepingXmmxbList) {
        this.cepingXmmxbObj[this.cepingXmmxbList[i].id] = this.cepingXmmxbList[i];
        if (this.zbCj < this.cepingXmmxbList[i].zbDj) {
          this.zbCj = this.cepingXmmxbList[i].zbDj;
        }
      }
      var zbList = []
      for (var i in this.cepingXmmxbList) {
        if (this.cepingXmmxbList[i].zbQz > 0) {
          let info = this.cepingXmmxbList[i];
          if (info.zbDj == 1) {
            info.zbName1 = info.zbMc;
            info.zbName2 = "";
            info.zbName3 = "";
          }
          if (info.zbDj == 2) {
            info.zbName1 = this.cepingXmmxbObj[info.pid].zbMc;
            info.zbName2 = info.zbMc;
            info.zbName3 = "";
          }
          if (info.zbDj == 3) {
            info.zbName1 = this.cepingXmmxbObj[this.cepingXmmxbObj[info.pid].pid].zbMc;
            info.zbName2 = this.cepingXmmxbObj[info.pid].zbMc;
            info.zbName3 = info.zbMc;
          }
          zbList.push(info)
        }
      }

      function compareZb(a, b) {
        return a.zbPx - b.zbPx;
      }

      var zzList = [];
      if (this.zbCj == 1) {
        zzList = zbList.sort(compareZb);
      }
      if (this.zbCj == 2) {
        var ls = [];
        for (var i in this.cepingXmmxbList) {
          if (this.cepingXmmxbList[i].pid == 0) {
            ls.push(this.cepingXmmxbList[i]);
          }
        }
        ls = ls.sort(compareZb);
        for (var j in ls) {
          var xls = []
          var qz = 0;
          for (var i in this.cepingXmmxbList) {
            if (this.cepingXmmxbList[i].pid == ls[j].id) {
              xls.push(this.cepingXmmxbList[i]);
              qz += this.cepingXmmxbList[i].zbQz;
            }
          }
          if (xls.length == 0) {
            ls[j].zbQz1 = ls[j].zbQz
            if (ls[j].zbQz > 0) {
              zzList.push(ls[j])
            }
          } else {
            var xxls = xls.sort(compareZb);
            for (var k in xxls) {
              xxls[k].zbQz1 = qz;
              if (xxls[k].zbQz > 0) {
                zzList.push(xxls[k])
              }
            }
          }
        }
      }
      if (this.zbCj == 3) {
        var ls = [];
        for (var i in this.cepingXmmxbList) {
          if (this.cepingXmmxbList[i].pid == 0) {
            ls.push(this.cepingXmmxbList[i]);
          }
        }
        ls = ls.sort(compareZb);
        for (var j in ls) {
          var xls = []
          var qz = 0;
          for (var i in this.cepingXmmxbList) {
            if (this.cepingXmmxbList[i].pid == ls[j].id) {
              xls.push(this.cepingXmmxbList[i]);
              qz += this.cepingXmmxbList[i].zbQz;
            }
          }
          if (xls.length == 0) {
            ls[j].zbQz1 = ls[j].zbQz
            if (ls[j].zbQz > 0) {
              zzList.push(ls[j])
            }
          } else {
            var xxls = xls.sort(compareZb);
            for (var k in xxls) {
              var xls1 = []
              var qz1 = 0;
              for (var ii in this.cepingXmmxbList) {
                if (this.cepingXmmxbList[ii].pid == xxls[k].id) {
                  xls1.push(this.cepingXmmxbList[ii]);
                  qz += this.cepingXmmxbList[ii].zbQz;
                  qz1 += this.cepingXmmxbList[ii].zbQz;
                }
              }
              if (xls1.length == 0) {
                xxls[k].zbQz1 = xxls[k].zbQz
                if (xxls[k].zbQz > 0) {
                  zzList.push(xxls[k])
                }
              } else {
                var xxls1 = xls1.sort(compareZb);
                for (var k1 in xxls1) {
                  xxls1[k1].zbQz1 = qz1;
                  if (xxls1[k1].zbQz > 0) {
                    zzList.push(xxls1[k1])
                  }
                }
              }
            }
          }
        }
      }
      //补充已有打分
      for (var i in zzList) {
        for (var j in this.cepingHdglXpxxDfjlbList) {
          if (this.cepingHdglXpxxDfjlbList[j].zbId == zzList[i].id) {
            zzList[i].fzId = this.cepingHdglXpxxDfjlbList[j].fzId;
            zzList[i].fzName = this.cepingHdglXpxxDfjlbList[j].fzName;
            zzList[i].fzFz = this.cepingHdglXpxxDfjlbList[j].fzFz;
            zzList[i].dfId = this.cepingHdglXpxxDfjlbList[j].id;
          }
        }
      }

      //console.log(zzList)
      //lin
      this.fzbList = [];
      for (var i in zzList) {
        if (this.fzbList.indexOf(zzList[i].zbName1) == -1) {
          this.fzbList.push(zzList[i].zbName1);
        }
      }
      ////console.log('zzList',zzList)
      // this.tableData = zzList;

      this.tableHeader = [];
      for (var i in this.cepingGzmxbList) {
        if(this.cepingGzmxbList[i].fzSsz){
          var find=this.tableHeader.find(item=>item==this.cepingGzmxbList[i].fzSsz);
          if(!find){
            this.tableHeader.push(this.cepingGzmxbList[i].fzSsz);
          }
        }
        // if(this.cepingGzmxbList[i].fzSsz){
        //   this.tableHeader.push(this.cepingGzmxbList[i].fzSsz);
        // }
      }
      // for (var i in this.cepingGzmxbList) {
      //   if (this.cepingGzmxbList[i].fzSsz == "优秀") {
      //     this.tableHeader.push("优秀");
      //     break;
      //   }
      // }
      // for (var i in this.cepingGzmxbList) {
      //   if (this.cepingGzmxbList[i].fzSsz == "较好") {
      //     this.tableHeader.push("较好");
      //     break;
      //   }
      // }
      // for (var i in this.cepingGzmxbList) {
      //   if (this.cepingGzmxbList[i].fzSsz == "一般") {
      //     this.tableHeader.push("一般");
      //     break;
      //   }
      // }
      // for (var i in this.cepingGzmxbList) {
      //   if (this.cepingGzmxbList[i].fzSsz == "较差") {
      //     this.tableHeader.push("较差");
      //     break;
      //   }
      // }

      function compareGz(a, b) {
        return a.fzPx - b.fzPx;
      }

      this.tableColumn = this.cepingGzmxbList.sort(compareGz);
      this.cepingDjmxb = {};
      if (this.cepingHdglXpxxDfjlbList != null & this.cepingHdglXpxxDfjlbList.length > 0) {
        this.cepingHdglXpxxDfjlbList.forEach(r => {
          if (r.dfLx == 1) {
            this.cepingDjmxb = r;
          }
        })
      }

      this.show = true;

    },
    objectSpanMethod({row, column, rowIndex, columnIndex}) {
      if (this.zbCj == 1) {
        return {
          rowspan: 1,
          colspan: 1
        };
      } else {
        if (columnIndex === 0) {
          if (rowIndex > 0 && this.tableData[rowIndex].zbName1 == this.tableData[rowIndex - 1].zbName1) {
            return {
              rowspan: 0,
              colspan: 1
            }
          } else {
            var j = 0;
            for (var i = rowIndex; i < this.tableData.length; i++) {
              if (this.tableData[rowIndex].zbName1 == this.tableData[i].zbName1) {
                j++;
              } else {
                break;
              }
            }
            return {
              rowspan: j,
              colspan: 1
            }
          }
        }
        if (this.zbCj == 3 && columnIndex === 1) {
          if (rowIndex > 0 && this.tableData[rowIndex].zbName2 == this.tableData[rowIndex - 1].zbName2 && this.tableData[rowIndex].zbName1 == this.tableData[rowIndex - 1].zbName1) {
            return {
              rowspan: 0,
              colspan: 1
            }
          } else {
            var j = 0;
            for (var i = rowIndex; i < this.tableData.length; i++) {
              if (this.tableData[rowIndex].zbName2 == this.tableData[i].zbName2 && this.tableData[rowIndex].zbName1 == this.tableData[i].zbName1) {
                j++;
              } else {
                break;
              }
            }
            return {
              rowspan: j,
              colspan: 1
            }
          }
        }
      }
    },

    getClass(i, len) {
      let x = i + 1;
      if (x / len <= 0.25) {
        return "pjyx";
      } else if (x / len <= 0.5) {
        return "pjjh";
      } else if (x / len <= 0.75) {
        return "pjyb";
      } else {
        return "pjjc";
      }
    },
    tableRowClassName({row, rowIndex}) {
      let fz = this.fzbList.indexOf(this.tableData[rowIndex].zbName1);
      if (fz % 2 == 0) {
        return 'cpRowNormal';
      } else {
        return 'cpRowstripe';
      }
      return '';
    }
  }
}
</script>

<style scoped>
.h_dw_time{
  text-align: left;
}
</style>

